/**
 * Created by jiaoyumin on 2017/9/29.
 */
import {
  IS_TOURISM,
  SET_USERINFO,
  SET_TOKEN,
  SUPPORT_INFO,
  PROCESS,
  AGENCY_INFO,
  SYS_INFO,
  WLJD_ARRAY,
  ZZCJ_ARRAY,
  ZZJD_ARRAY,
  REPORT_DETAIL,
  ANY_LISIS,
  PROVINCES,
  CITIES,
  AREAS,
  REPORT_LIST,
  TOURISM_PROCESS,
  AGENCYLIST,
  STOPED_AGENCYLIST,
  PROCESS_LIST,
  GET_TOPMENU,
  GOV_REPORT_DETAIL,
  GOV_REPORT_GROUP_LEVEL
} from "./mutation-types";

export default {
  [IS_TOURISM](state, { flag }) {
    state.istourism = flag;
    if (flag) {
      state.currentMenus = state.tamenus;
    } else {
      state.currentMenus = state.govmenus;
    }
  },
  [SET_USERINFO](state, { data }) {
    if (data) {
      state.userinfo = data;
      sessionStorage.setItem("userinfo", JSON.stringify(data));
    }
  },
  [SET_TOKEN](state, token) {
    if (token) {
      state.token = token;
      sessionStorage.setItem("token", token);
    }
  },
  [SUPPORT_INFO](state, { data }) {
    state.supportinfo = data;
  },
  [PROCESS](state, { data }) {
    const process_status = {};
    if (data) {
      switch (data.state) {
        case 0:
          process_status.type = "gray";
          process_status.color = "#606266";
          process_status.title = "尚未提交";
          break;
        case 1:
          process_status.type = "primary";
          process_status.color = "#409EFF";
          process_status.title = "等待审核";
          break;
        case 2:
          process_status.type = "success";
          process_status.color = "#67C23A";
          process_status.title = "审核通过";
          break;
        case 3:
          process_status.type = "danger";
          process_status.color = "#F56C6C";
          process_status.title = "退回";
          break;
        default:
          break;
      }
      process_status.state = data.state;
    }
    state.process = process_status;
  },
  [SYS_INFO](state, { data }) {
    if (data) {
      sessionStorage.setItem("sysinfo", JSON.stringify(data));
      state.sysinfo = data;
      if (!state.istourism && data.govState !== 0) {
        state.currentMenus = state.permissionsMenu;
      }
    }
  },
  [AGENCY_INFO](state, { data }) {
    if (data) {
      state.agencyinfo = data;
      sessionStorage.setItem("agencyinfo", JSON.stringify(data));
    }
  },
  [WLJD_ARRAY](state, data) {
    state.wljds = [...data];
  },
  [ZZJD_ARRAY](state, data) {
    state.zzjds = [...data];
  },
  [ZZCJ_ARRAY](state, data) {
    state.zzcjs = [...data];
  },
  [REPORT_DETAIL](state, { data }) {
    if (data) {
      sessionStorage.setItem("reportDetail", JSON.stringify(data));
      state.reportDetail = data;
    }
  },
  [ANY_LISIS](state, { data }) {
    if (data) {
      sessionStorage.setItem("chartsData", JSON.stringify(data));
      state.chartsData = data;
    }
  },
  [PROVINCES](state, data) {
    if (data) {
      state.provinces = data;
      localStorage.setItem("provinces", JSON.stringify(data));
    }
  },
  [CITIES](state, data) {
    if (data) {
      state.cities = data;
      localStorage.setItem("cities", JSON.stringify(data));
    }
  },
  [AREAS](state, data) {
    if (data) {
      state.areas = data;
      localStorage.setItem("areas", JSON.stringify(data));
    }
  },
  [REPORT_LIST](state, { data }) {
    if (data) {
      data.map(item => {
        if (
          ((item.cityState === 1 || item.cityState === -2) &&
          (item.countryState === 1 || item.countryState === -2) &&
          (item.provinceState === 1 || item.provinceState === -2)) && item.state > -1
        ) {
          item.withdrawState = 1;
        }
      });
      state.reportList = data;
    }
  },
  [TOURISM_PROCESS](state, { data }) {
    if (data) {
      sessionStorage.setItem("govprocess", JSON.stringify(data));
      state.govprocess = data;
      state.govprocess.completeFillPercentage =
        data.localReportedNum / data.totalNum === 0
          ? 0
          : (data.localReportedNum / data.totalNum).toFixed(2) * 100;
      state.govprocess.finishedPercentage =
        data.localFinishedNum / data.totalNum === 0
          ? 0
          : (data.localFinishedNum / data.totalNum).toFixed(2) * 100;
    }
  },
  [AGENCYLIST](state, { data }) {
    if (data) {
      data.rows.map(item => {
        if (item.Reports && item.Reports.length > 0) {
          const {
            id: reportid,
            cityState,
            countryState,
            provinceState,
            season,
            state: reportState,
            year
          } = item.Reports[0];
          Object.assign(item, {
            reportid,
            cityState,
            countryState,
            provinceState,
            season,
            reportState,
            year
          });
          delete item.Reports;
        }
      });
      state.agencyList = data;
    }
  },
  [STOPED_AGENCYLIST](state, { data }) {
    if (data) {
      state.stopedAgencyList = data;
    }
  },
  [GOV_REPORT_DETAIL](state, { data }) {
    if (data) {
      sessionStorage.setItem("reportGovDetail", JSON.stringify(data));
      state.reportGovDetail = data;
    }
  },
  [PROCESS_LIST](state, { data }) {
    if (data) {
      data.map(item => {
        item.totalNum = item.data.totalNum;
        item.stopedNum = item.data.stopedNum;
        item.reportedNum = item.data.reportedNum;
        item.returnedNum = item.data.returnedNum;
        item.finishedNum = item.data.finishedNum;
      });
      state.processList = data;
    }
  },
  [GOV_REPORT_GROUP_LEVEL](state,level){
    state.auditLevel = level;
  }
};
